<template>
  <div class="login">
    <div class="left">
      <!-- 登录面板标题部分 -->
      <div class="header">
        <img class="header-img" src="@/assets/img/login_logo.png" alt />
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登录</span>
      </div>
      <!-- 登录面板表单部分, 2:设置登录信息验证 -->
      <!-- 
          验证步骤：
            1.要再el-form 标签上设置model, rules 如：:model="对象", :rules="对象"
            2.在每个子项上面 el-form-item标签上设置 一个prop属性
              * prop属性的值 要和对应表单元素的v-model的值要映射起来
            3.在data对象里配置每个值的验证规则
            // 验证整个表单校
                1.给el-form表单设置ref属性 ref="form"
                2.在js部分使用this.$refs.值 获取el-fomr组件的实例
                3.el-fomr组件的实例中有一个validate方法，是对整个表单进行校验的方法
                // validate是一个方法，它有一个回调函数，参数1：boolean 参数2：object
      -->
      <el-form :model="form" :rules="rules" ref="form" class="form">
        <el-form-item prop="phone">
          <el-input v-model="form.phone" prefix-icon="el-icon-user" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input v-model="form.code" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
            </el-col>
            <el-col :span="8">
              <img
                v-if="bol"
                class="form-img"
                :src="baseURL+'/captcha?type=login'"
                @click="updateCodeImg"
                alt
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            <span class="form-bad">我已阅读并同意</span>
            <el-link type="primary">用户协议</el-link>
            <span>和</span>
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="form-btn" @click="submit">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="form-btn" @click="showRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 右边图片 -->
    <img src="@/assets/img/login_banner.png" alt />
    <!-- register组件 -->
    <!-- 获取register组件的实例 将isShow的值改为true -->
    <register ref="register"></register>
  </div>
</template>
<script>
import register from "@/views/login/register";
import { toLogin } from "@/api/login.js";
import { saveLocal, getLocal } from "@/utils/local.js";
export default {
  components: {
    register
  },
  data() {
    return {
      baseURL: process.env.VUE_APP_URL,
      bol: true,
      form: {
        phone: "", //是	string	手机号
        password: "", //是	string	密码
        code: "", //是	string	验证码
        isPass: ""
      },
      rules: {
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "change"
          },
          {
            validator: (rule, value, callback) => {
              let _phone = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_phone.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
            trigger: "change"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 12, message: "请输入6-12位的密码", trigger: "change" }
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "请输入4位验证码", trigger: "change" }
        ],
        isPass: [
          { required: true, message: "请勾选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value === true) {
                callback();
              } else {
                callback(new Error("请勾选协议"));
              }
            }
          }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate(result => {
        if (result) {
          // this.$message.success("验证通过!");
          toLogin(this.form).then(res => {
            // 登录成功 保存token
            // 提示用户 跳转到首页
            saveLocal(res.data.token);
            this.$message.success("登录成功!");
            this.$router.push("/layout");
          });
        }
      });
    },
    showRegister() {
      this.$refs.register.isShow = true;
    },
    updateCodeImg() {
      this.bol = false;
      this.$nextTick(() => {
        this.bol = true;
      });
    }
  },
  created() {
    // 如果本地有token  跳转到主页
    if (getLocal() != undefined) { 
      this.$router.push("/layout");
    }
  }
};
</script>
<style lang="less" scoped>
.login {
  height: 100%;
  background: linear-gradient(to right, #1494fa, #01c6fa);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .header {
      display: flex;
      align-items: center;
      .header-img {
        width: 22px;
        height: 17px;
        margin-right: 20px;
      }
      .t1 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .t2 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .line {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin: 0 20px;
      }
    }
    .form {
      margin-top: 25px;
      .form-img {
        width: 100%;
        height: 40px;
      }
      .form-btn {
        width: 100%;
      }
      .form-bad {
        vertical-align: -1px;
      }
    }
  }
}
</style>